//
//  SFGiftThatTableViewCell.m
//  GiftThat
//
//  Created by dllo on 16/1/8.
//  Copyright © 2016年 ChuXiang. All rights reserved.
//

#import "SFGiftThatTableViewCell.h"
#define Labwidth [UIScreen mainScreen].bounds.size.width
@interface SFGiftThatTableViewCell ()
@property (nonatomic,assign) NSInteger number;
@property (nonatomic,strong)   UIImageView *underImage;
@end


@implementation SFGiftThatTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self GetData];
        self.BTbool = NO;
        
    }
    return self;
}
-(void)GetData{
    self.imageViews = [[UIImageView alloc] init];
    self.imageViews.userInteractionEnabled = YES;
    [self addSubview:self.imageViews];
    self.underImage = [[UIImageView alloc]init];
    self.underImage.image = [UIImage imageNamed:@"under"];
    [self addSubview:self.underImage];
    
    self.label = [[UILabel alloc] init];
    self.label.textColor = [UIColor whiteColor];
    self.label.font = [UIFont systemFontOfSize:15];
    self.label.numberOfLines = 0;
 
    [self addSubview:self.label];
    
    self.ShouCangViewAndLb = [[UIImageView alloc]init];
    self.ShouCangViewAndLb.userInteractionEnabled = YES;
    [self.imageViews addSubview:self.ShouCangViewAndLb];
    
//    self.ShouCangview = [[UIImageView alloc]init];
    self.button = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.button addTarget:self action:@selector(buttonDidPress:) forControlEvents:UIControlEventTouchUpInside];
    [self.button setBackgroundImage:[UIImage imageNamed:@"aixin"] forState:UIControlStateNormal];

    [self.ShouCangViewAndLb addSubview:self.button];
    
    
    self.ShouCanglabel = [[UILabel alloc]init];
    
    [self.ShouCangViewAndLb addSubview:self.ShouCangview];
    [self.ShouCangViewAndLb addSubview:self.ShouCanglabel];

    self.ShouCangViewAndLb.backgroundColor = [UIColor blackColor];
    self.ShouCangViewAndLb.alpha = 0.8;



   
    

   
}

-(void)setSelectionCellModel:(SFSelectionCellModel *)SelectionCellModel{
    if (_SelectionCellModel != SelectionCellModel) {
        _SelectionCellModel = SelectionCellModel;
      
    }

    
    [self.imageViews sd_setImageWithURL:[NSURL URLWithString:self.SelectionCellModel.cover_image_url]placeholderImage:[UIImage imageNamed:@"holder"]];
    self.imageViews.layer.cornerRadius = 10;
    self.imageViews.clipsToBounds = YES;
    
     self.label.text = _SelectionCellModel.title;
       self.ShouCanglabel.textColor = [UIColor whiteColor];

    
    }
//判断收藏图片状态
-(void)KeyValue{
    SAKeyValueStore *store = [[SAKeyValueStore alloc]initDBWithName:@"SFstore.db"];

    NSString *tabelName = @"like";
    [store createTableWithName:tabelName];
    NSArray *array = [store getAllItemsFromTable:tabelName];
    if (array.count) {
        NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:0];
        
        NSMutableArray *modelArray = [NSMutableArray arrayWithCapacity:0];
        
        for (SAKeyValueItem *item in array) {
            NSString *string = [NSString stringWithString:item.itemId];
            SFSelectionCellModel *model = [[SFSelectionCellModel alloc]initWithDictionary:item.itemObject];
          
//      modelArray装所有元素
            [modelArray addObject:model];
//       mutableArray 装所有元素的id
            [mutableArray addObject:string];
          
            for (int i = 0; i < mutableArray.count; i++) {
                if ([_SelectionCellModel.NewId isEqualToString:mutableArray[i]]) {
                
                    self.BTbool = YES;

                    self.ShouCanglabel.textAlignment = NSTextAlignmentCenter;
                    [self.ShouCanglabel setFont:[UIFont fontWithName:@"FZLanTingHei-EL-GBK" size:10]];
           
                    if ([_SelectionCellModel.likes_count floatValue] / 1000 >0) {
                        
                        float count = [_SelectionCellModel.likes_count floatValue] / 1000;
                        
                        NSString *string = [NSString stringWithFormat:@"%.1f%@",count ,@"k"];
                        self.ShouCanglabel.text = string;

                        
                    }else{
                   
                        self.ShouCanglabel.text = [_SelectionCellModel.likes_count stringValue];

                    }

                    
                    [_button setBackgroundImage:[UIImage imageNamed:@"Redaixin"] forState:UIControlStateNormal];
                    break;
                 
                }else{
                    self.BTbool = NO;
                    self.ShouCanglabel.textColor = [UIColor whiteColor];
                    self.ShouCanglabel.textAlignment = NSTextAlignmentCenter;
                    [self.ShouCanglabel setFont:[UIFont fontWithName:@"FZLanTingHei-EL-GBK" size:10]];
                    
                    
                    if ([_SelectionCellModel.likes_count floatValue] / 1000 >0) {
                        float count = [_SelectionCellModel.likes_count floatValue] / 1000;
                        NSString *string = [NSString stringWithFormat:@"%.1f%@",count,@"k"];
                        self.ShouCanglabel.text = string;
                    [_button setBackgroundImage:[UIImage imageNamed:@"Nilaixin"] forState:UIControlStateNormal];
                      
                    }else{
                    
                    self.ShouCanglabel.text = [_SelectionCellModel.likes_count stringValue];
                    [_button setBackgroundImage:[UIImage imageNamed:@"Nilaixin"] forState:UIControlStateNormal];
                    }
                }
                
            }
            
        }
        
    }else{
        
        self.ShouCanglabel.textAlignment = NSTextAlignmentCenter;
        self.number = _SelectionCellModel.likes_count.integerValue;
      
        if (self.number / 1000 > 0) {
            float count = [_SelectionCellModel.likes_count floatValue] / 1000;
            NSString *string = [NSString stringWithFormat:@"%.1f%@",count,@"k"];
            self.ShouCanglabel.text = string;
        }else{
        self.ShouCanglabel.text = [NSString stringWithFormat:@"%ld",(long)self.number];
        }
        
        [self.ShouCanglabel setFont:[UIFont fontWithName:@"FZLanTingHei-EL-GBK" size:10]];
//        self.ShouCanglabel.textColor = [UIColor whiteColor];
      
        [self.button setBackgroundImage:[UIImage imageNamed:@"Nilaixin"] forState:UIControlStateNormal];
    }

}



-(void)buttonDidPress:(UIButton *)sender{
 
    if (self.BTbool == NO) {
        SAKeyValueStore *store = [[SAKeyValueStore alloc]initDBWithName:@"SFstore.db"];
        NSString *tableName = @"like";
        [store createTableWithName:tableName];
     
        if ([_SelectionCellModel.likes_count floatValue]/ 1000 > 0) {
        
            float count = ([_SelectionCellModel.likes_count floatValue] + 1) / 1000 ;
     
    
           self.ShouCanglabel.text = [NSString stringWithFormat:@"%.1f%@",count,@"k"];
          
        }else{
            self.ShouCanglabel.text = [NSString stringWithFormat:@"%ld",(long)self.number];
        }
        
        
        
        
        NSDictionary *dic = @{@"title":_SelectionCellModel.title,@"cover_image_url":_SelectionCellModel.cover_image_url,@"url":_SelectionCellModel.url,@"likes_count":[NSString stringWithFormat:@"%ld",(long)self.number]};
      
        [store putObject:dic withId:_SelectionCellModel.NewId intoTable:tableName];
        [store close];
        [self.button setBackgroundImage:[UIImage imageNamed:@"Redaixin"] forState:UIControlStateNormal];

        self.BTbool = YES;
    
    }else{
        
        SAKeyValueStore *store = [[SAKeyValueStore alloc]initDBWithName:@"SFstore.db"];
        NSString *tableName = @"like";
        [store createTableWithName:tableName];
        [store deleteObjectById:_SelectionCellModel.NewId fromTable:tableName];
        [self.button setBackgroundImage:[UIImage imageNamed:@"Nilaixin"] forState:UIControlStateNormal];

     
        if (_SelectionCellModel.likes_count.floatValue / 1000 > 0) {
            float count = _SelectionCellModel.likes_count.floatValue / 1000;

            NSString *string = [NSString stringWithFormat:@"%.1f%@",count,@"k"];
            self.ShouCanglabel.text = string;
        }else{
            self.ShouCanglabel.text = [NSString stringWithFormat:@"%ld",(long)self.number];
        }

          self.BTbool = NO;
    }
    

    
}

-(void)layoutSubviews{
    [super layoutSubviews];
    self.imageViews.frame = CGRectMake(5, 5, self.width - 10, self.height - 10);
    self.underImage.frame = CGRectMake(0, self.height - 60, self.width, 60);
    CGFloat labelHeight = 0;
    if (_SelectionCellModel.title.length > 0) {
        labelHeight = [SFGiftThatTableViewCell hightWithLable:_SelectionCellModel.title];
    }
    self.label.frame = CGRectMake(10, self.contentView.bottom - labelHeight - 10, Labwidth - 20, labelHeight);
    self.ShouCangViewAndLb.frame = CGRectMake(self.width - 60, 0, 40, 40);
    self.button.frame = CGRectMake(5, 0, 30, 25);
    self.ShouCanglabel.frame = CGRectMake(0, self.ShouCangViewAndLb.height - 15, self.ShouCangViewAndLb.width , 15);

    self.unserimageview.frame = CGRectMake(5, self.height - 45, self.width - 10, 40);
     [self KeyValue];
    
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"Color"] isEqualToString:@"BlackColor"]) {
        self.label.textColor = [UIColor whiteColor];
         self.ShouCanglabel.textColor = [UIColor whiteColor];
        self.backgroundColor = [UIColor colorWithRed:37 / 255.0 green:36 / 255.0 blue:40 / 255.0 alpha:1.0];
        
    }else{
//        self.label.textColor = [UIColor blackColor];
//        self.ShouCanglabel.textColor = [UIColor blackColor];
        self.backgroundColor = [UIColor whiteColor];
        
    }


}

+(CGFloat)hightWithLable:(NSString *)lable{
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:15]};
    CGSize size = CGSizeMake(Labwidth - 20, CGFLOAT_MAX);
    CGRect rect = [lable boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.height;
  
}


















- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
